package com.casic.cloud.controller;

import com.casic.cloud.service.ResumeFeignClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @program: rare_earth_cloud
 * @description: 收发控制器，仅用于演示
 * @author: Tan_sir
 * @create: 2021-05-06 11:46
 **/

@RestController
@RequestMapping("/autodeliver")
public class AutodeliverController {



   @Autowired
   private ResumeFeignClient resumeFeignClient;

    @Autowired
    private DiscoveryClient discoveryClient;


    /**
     * 从Eureka获取实例端口信息,使用Ribbon负载均衡
     *
     * @param userId
     * @return
     */
    /*@GetMapping("/checkState/{userId}")
    public Integer findResumeOpenState(@PathVariable Long userId) {

        String url = "http://rare-earth-service-resume/resume/openstate/" + userId; //指定路径

        Integer forObject = restTemplate.getForObject(url, Integer.class);
        System.out.println(url);
        System.out.println("======>>>当前状态为:" + forObject);
        return forObject;
    }*/


    /*http://localhost:8090/autodeliver/checkState/1545132?access_token=d256bb66-9d94-4422-95e6-0e1a8d873024*/
    /**
     * 提供者模拟处理超时，调用方法添加Hystrix控制
     *
     * @param userId
     * @return
     */
    @HystrixCommand(
            //线程池标识，要保持唯一，不唯一的话就共用
            threadPoolKey = "findResumeOpenStateTomeoutFallback",
            //线程池属性细节
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "1"),//线程数
                    @HystrixProperty(name = "maxQueueSize", value = "20")//队列长度
            },
            /**
             * 8秒钟内，请求次数达到2个，并且失败率在50%以上，就跳闸
             * 跳闸后活动窗⼝设置为3s
            */
            //commandProperties熔断的细节属性控制
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000"),
                    //统计时间窗口定义
                    @HystrixProperty(name =
                            "metrics.rollingStats.timeInMilliseconds",value = "8000"),
                    //统计时间窗口最小请求数
                    @HystrixProperty(name =
                            "circuitBreaker.requestVolumeThreshold",value = "2"),
                    //统计时间窗口内的错误数量百分比阙值
                    @HystrixProperty(name =
                            "circuitBreaker.errorThresholdPercentage",value = "50"),
                    //自我修复时的活动窗口长度
                    @HystrixProperty(name =
                            "circuitBreaker.sleepWindowInMilliseconds",value = "3000")
            },
            fallbackMethod = "fallBack" //回退方法
    )
    @GetMapping("/checkStateTomeoutFallback/{userId}")
    public Integer findResumeOpenStateTomeoutFallback(@PathVariable Long userId) {

        Integer defaultResumeState = resumeFeignClient.findDefaultResumeState(userId);
        System.out.println("======>>>当前状态为:" + defaultResumeState);
        return defaultResumeState;
    }

    public Integer fallBack(Long userId) {
        return -1;//兜底数据
    }


}

